package com.hnqcgczy.supplier.controller;

import com.hnqcgczy.supplier.service.EvaluateService;
import com.hnqcgczy.supplier.service.impl.EvaluateServiceImpl;
import com.hnqcgczy.supplier.utils.BaseServlet;
import com.hnqcgczy.supplier.vo.Evaluate;
import com.hnqcgczy.supplier.vo.EvaluateVo;
import com.mysql.cj.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "EvaluateServlet", value = "/EvaluateServlet")
public class EvaluateServlet extends BaseServlet {
    private EvaluateService evaluateService = new EvaluateServiceImpl();


    public void addEvaluate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        Evaluate evaluate = new Evaluate();
        evaluate.setCompanyId(Integer.valueOf(request.getParameter("company_id")));
        evaluate.setAdminName(request.getParameter("admin_name"));
        evaluate.setEvaluateInfor(request.getParameter("evaluate_infor"));
        evaluate.setScore(Double.valueOf(request.getParameter("score")));
        evaluate.setOrderId(Integer.valueOf(request.getParameter("order_id")));
        evaluate.setDate(Date.valueOf(request.getParameter("date")));
        boolean flag = evaluateService.add(evaluate);
        String msg = flag ? "添加成功" : "添加失败";
        request.setAttribute("msg", msg);
        //转发
        request.getRequestDispatcher("msg.jsp").forward(request, response);
    }

    public void queryAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        String companyName = request.getParameter("companyName");
        String adminName = request.getParameter("adminName");
        String date = request.getParameter("date");
        String orderId = request.getParameter("orderId");
        //判断
        if (StringUtils.isNullOrEmpty(companyName)) {
            companyName = "空";
        }
        if (StringUtils.isNullOrEmpty(adminName)) {
            adminName = "空";
        }
        if (StringUtils.isNullOrEmpty(date)) {
            date = "1970-01-01";
        }
        if (StringUtils.isNullOrEmpty(orderId)) {
            orderId = "0";
        }

        //查询条件
        EvaluateVo query = new EvaluateVo();
        query.setCompanyName(companyName);
        query.setAdminName(adminName);
        query.setDate(Date.valueOf(date));
        query.setOrderId(Integer.valueOf(orderId));

        String pNow = request.getParameter("pageNow");
        if (StringUtils.isNullOrEmpty(pNow)) {
            pNow = "1";
        }
        int pageNow = Integer.valueOf(pNow);
        int pageSize = 10;

        int totals = evaluateService.totals(query);
        if (pageNow <= 1) {
            pageNow = 1;
        }
        if (pageNow >= totals && totals != 0) {
            pageNow = totals;
        }
        List<EvaluateVo> evaluateVos = evaluateService.findByLike(query, pageNow, pageSize);
        int pages = totals % pageSize == 0 ? totals / pageSize : totals / pageSize + 1;
        //存
        request.setAttribute("pageNow", pageNow);
        request.setAttribute("evaluateVos", evaluateVos);
        request.setAttribute("totals", totals);
        request.setAttribute("pages", pages);
        //跳
        request.getRequestDispatcher("Assessing.jsp").forward(request, response);
    }

    public void updateById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        Evaluate evaluate = new Evaluate();
        evaluate.setEvaluateId(Integer.valueOf(request.getParameter("evaluate_id")));
        evaluate.setCompanyId(Integer.valueOf(request.getParameter("company_id")));
        evaluate.setAdminName(request.getParameter("admin_name"));
        evaluate.setEvaluateInfor(request.getParameter("evaluate_infor"));
        evaluate.setScore(Double.valueOf(request.getParameter("score")));
        evaluate.setOrderId(Integer.valueOf(request.getParameter("order_id")));
        evaluate.setDate(Date.valueOf(request.getParameter("date")));
        boolean flag = evaluateService.update(evaluate);
        String msg = flag ? "修改成功" : "修改失败";
        request.setAttribute("msg", msg);
        //转发
        request.getRequestDispatcher("msg.jsp").forward(request, response);
    }

    public void deleteById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        boolean flag = evaluateService.delete(Integer.valueOf(request.getParameter("evaluate_id")));
        String msg = flag ? "删除成功" : "删除失败";
        request.setAttribute("msg", msg);
        //转发
        request.getRequestDispatcher("msg.jsp").forward(request, response);
    }
}